package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Role;
import com.gxa.zhonglong.service.RoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ws
 * @date 2021/10/21 10:38
 */
@Api(tags ="角色操作接口api")
@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @PostMapping("/delete")
    @ApiOperation("通过id删除角色的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "角色编号",required = true,dataType = "long",paramType = "query")
    })
    public Map<String,Object> delete(@ApiParam("id") Long id) throws BusinessException {
        roleService.delete(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改角色的方法")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "role",value = "角色对象",dataType = "Role",paramType = "body")
//    })
    public Map<String,Object> update(@RequestBody Role role) throws BusinessException {
        roleService.update(role);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/add")
    @ApiOperation("添加角色的方法")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "role",value = "角色对象",required = true,dataType = "Role",paramType = "body")
//    })
    public Map<String,Object> add( Role role) throws BusinessException {
        roleService.add(role);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getAll")
    @ApiOperation("查询所有角色的方法")
    public Map<String,Object> getAll(){
        List<Map<String, Object>> all = roleService.getAll();

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(all);
        return jsonResult.getDataMap();
    }






}
